import 'package:eshi/app/shi/widget/CommonWidgets.dart';
import 'package:flutter/material.dart';
import 'package:eshi/app/CommonFunctions.dart';
import 'package:eshi/main.dart';
import 'package:eshi/app/shi/data/History.dart';
import 'package:eshi/app/shi/data/HistoryPoint.dart';
import 'package:eshi/app/shi/data/parse/FileParser.dart';
import 'package:eshi/app/shi/data/tabtxt_history.dart';
import 'package:eshi/app/shi/taxis/TabEditPage.dart';

class tools extends StatelessWidget {
  History history;
  bool isHorizontal;
  tools(this.isHorizontal,this.history, {super.key});

  @override
  Widget build(BuildContext context) {

    return Container(
        padding: EdgeInsets.all(10),
        child:  isHorizontal
          ? Row(
            mainAxisSize: MainAxisSize.min,
            children: buttons(context),
          ) 
          : Column(
              mainAxisSize: MainAxisSize.min,
              children: buttons(context),
          )
    );
  }
  
  List<Widget> buttons(context){
        return [
          FloatingActionButton(
            onPressed:  (){
              customWidgetKey.currentState?.onShare(history);
            },
            tooltip: mt('导出'),
              // 自定义形状 - 使用更精确的圆形边界
            shape: const CircleBorder(
              side: BorderSide.none, // 无边框
            ),
            child: const Icon(Icons.share),
          ),
          SizedBox(width: 10,height: 10,), // 间距为10
          FloatingActionButton(
            onPressed:  () {
                modeWindow(context, TabEditPage(
                  title: mt("新增"), 
                  data: "", 
                  titleCanEdit: true,
                  onChange:(v,marks,name) async{
                        //2位数字结尾的时候就认为是增加节点  
                        if(RegExp(r"\d{2}$").hasMatch(name)){
                          final point = HistoryPoint({})
                            ..name=name
                            ..history= history;
                          history.data.add(point);
                          customWidgetKey.currentState?.onPointChange(point,data:v,marks: marks);
                        }else{
                          try{
                              // if(TabTxtHistory(v).data.length<2){
                              //   throw Exception("内容解析失败");
                              // }
                              if(await FileParser.assetsFileExists("$name.txt")){
                                throw Exception("文件已经存在");
                              }
                          }catch(e){
                            msgErr(e.toString());
                            return ;
                          }
                          TabTxtHistory.update(name,v,marks:marks);
                          mainWidgetKey.currentState?.changeHistory(await TabTxtHistory.load(name));
                        }

                        Navigator.pop(context);
                    }
                  ));
            },
            tooltip: mt('添加新的时间点'),
              // 自定义形状 - 使用更精确的圆形边界
            shape: const CircleBorder(
              side: BorderSide.none, // 无边框
            ),
            child: const Icon(Icons.add),
          ),
          SizedBox(width: 10,height: 10,), // 间距为10
          FloatingActionButton(
            onPressed:  () {
                  Navigator.pushNamed(context, '/settings');
            },
            tooltip: mt('配置'),
              // 自定义形状 - 使用更精确的圆形边界
            shape: const CircleBorder(
              side: BorderSide.none, // 无边框
            ),
            child: const Icon(Icons.settings),
          ),
        ];
  }
}